﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;
using Demo.Mmose.Core.Util;
#endregion

namespace Demo.Mmose.Core.Map
{
    /// <summary>
    /// 
    /// </summary>
    internal class MapEnumerable<T> : IEnumerable<T>, IDisposable
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pooledEnumerator"></param>
        private MapEnumerable( IEnumerator<T> enumerator )
        {
            m_Enumerator = enumerator;
        }
        #endregion

        #region zh-CHS 共有静态方法 | en Public Static Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pooledEnumerator"></param>
        /// <returns></returns>
        public static MapEnumerable<T> Instantiate( IEnumerator<T> enumerator )
        { 
            return new MapEnumerable<T>( enumerator );
        }
        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// m_Enumerator是初始化构造时的实例
        /// </summary>
        private IEnumerator<T> m_Enumerator = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<T> GetEnumerator()
        {
            return m_Enumerator;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return m_Enumerator;
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 只在Release(...)中改变
        /// </summary>
        private int m_bIsRelease = 0;
        #endregion
        /// <summary>
        /// 释放自己(数据不再有效)
        /// </summary>
        public void Release()
        {
            if ( Interlocked.Exchange( ref m_bIsRelease, 1 ) == 1 )
                return;

            m_Enumerator.Dispose();
        }

        /// <summary>
        /// none
        /// </summary>
        public void Dispose()
        {
            Release();
        }
        #endregion
    }
}
#endregion

